/*
 * Copyright (c) 2008 Junseok Kim
 * Author: Junseok Kim <jskim@usn.konkuk.ac.kr> <http://usn.konkuk.ac.kr/~jskim>
 * Date: 2008/05/30
 * Version: 0.0.1
 * Published under the terms of the GNU General Public License (GPLv2).
 */
#include "pr.h" 
#include "AODV25nodeTest.h"
module AODV25nodeTestM {
  uses {
    interface Boot;
    interface SplitControl;
    interface Timer<TMilli> as MilliTimer;
    interface Packet;
    interface AMSend;
    interface Receive;
    interface Leds;
  }
}
implementation {
  
  message_t pkt;
  message_t* p_pkt;
  
  uint16_t src  = 0;
  uint16_t dest = 1;
  uint8_t seqno = 0;
  
  event void Boot.booted() {
    call SplitControl.start();
  }
  
  
  event void SplitControl.startDone(error_t err) {
    if (err == SUCCESS) {
      dbg("APPS", "%s\t APPS: startDone %d.\n", sim_time_string(), err);
      p_pkt = &pkt;
      if( TOS_NODE_ID == src )
	  {
	    pr("find src \n ");
        call MilliTimer.startPeriodic(1024*10);
	  }
	  if(TOS_NODE_ID == dest)
	  {
		call Leds.set(7);
	  }
    } else {
      call SplitControl.start();
    }
  }
  
  event void SplitControl.stopDone(error_t err) {
    // do nothing
  }	
  
  event void MilliTimer.fired() {
    error_t err = 0;
    aodv_test_msg * testmsg;
    testmsg = (aodv_test_msg *)call Packet.getPayload(&pkt, sizeof(aodv_test_msg));
    testmsg->seqno= seqno;
	dbg("APPS", "%s\t APPS: MilliTimer.fired()\n", sim_time_string());
    err = call AMSend.send(dest, &pkt, sizeof(aodv_test_msg));
	// pr("err :%d\n",err);
  }
  
  event void AMSend.sendDone(message_t* bufPtr, error_t error) {
	//in test mordle it will not fired the sendDone
	if(error==SUCCESS){
		pr("success to send the data %d\n",seqno);
	} else{
		pr("faild to send the data %d\n",seqno);
	}
	seqno++;
    dbg("APPS", "%s\t APPS: sendDone!!\n", sim_time_string());
  }

  event message_t* Receive.receive(message_t* bufPtr, void* payload, uint8_t len) {
    	aodv_test_msg* testmsg= (aodv_test_msg *)payload;
	pr("receive packet NO:%d in top\n", testmsg->seqno);
	    dbg("APPS", "%s\t APPS: receive!!\n", sim_time_string());

	call Leds.led1Toggle();
    return bufPtr;
  }
}

